/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connections;

import Object.Album;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jboss.weld.bean.builtin.CallableMethodHandler;

/**
 *
 * @author nguyen
 */
public class AlbumDB {

    private String url;
    private String user;
    private String password;

    public AlbumDB(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public boolean CreateAlbum(Album al) {
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call createAlbum (?, ?, ?, ?)}");
            cstmt.setString(1, al.getAlbumname());
            cstmt.setString(2, al.getDescription());
            cstmt.setString(3, al.getDatecreate());
            cstmt.setString(4, al.getUsername());
            int rows = cstmt.executeUpdate();
            conn.close();
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
     public List<Album> getAlbum() {
        List<Album> listAlbum = new ArrayList();
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call showAlbum()}");
            ResultSet rs = cstmt.executeQuery();
            while (rs.next()) {
                int albumid = rs.getInt(1);
                String albumname = rs.getString(2);
                String description = rs.getString(3);
                String datecreate = rs.getString(4);
                String username = rs.getString(5);
                Album a = new Album(albumid, albumname, description, datecreate, username);
                listAlbum.add(a);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return listAlbum;
    }
}
